/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZGrant.GranteeType;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.GuestAccount;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.mailbox.ACL;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.acl.FolderACL;
/*     */ import com.zimbra.cs.memcached.MemcachedConnector;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestFolderACLCache
/*     */   extends TestCase
/*     */ {
/*     */   String OWNER_ACCT_ID;
/*     */   Account USER1;
/*     */   Account USER2;
/*     */   Account USER3;
/*     */   int INBOX_FID;
/*     */   int SUB1_FID;
/*     */   int SUB2_FID;
/*     */   int SUB3_FID;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  97 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  99 */     Account ownerAcct = prov.get(Key.AccountBy.name, "user1");
/* 100 */     ZMailbox ownerMbx = TestUtil.getZMailbox(ownerAcct.getName());
/*     */     
/* 102 */     ZFolder inbox = ownerMbx.getFolderByPath("inbox");
/*     */     
/* 104 */     String sub1Path = "/inbox/sub1";
/* 105 */     ZFolder sub1 = ownerMbx.getFolderByPath(sub1Path);
/* 106 */     if (sub1 == null) {
/* 107 */       sub1 = TestUtil.createFolder(ownerMbx, sub1Path);
/* 108 */       ownerMbx.modifyFolderGrant(sub1.getId(), ZGrant.GranteeType.usr, "user2", "w", null);
/* 109 */       ownerMbx.modifyFolderGrant(sub1.getId(), ZGrant.GranteeType.usr, "user3", "rw", null);
/*     */     }
/*     */     
/* 112 */     String sub2Path = "/inbox/sub1/sub2";
/* 113 */     ZFolder sub2 = ownerMbx.getFolderByPath(sub2Path);
/* 114 */     if (sub2 == null) {
/* 115 */       sub2 = TestUtil.createFolder(ownerMbx, sub2Path);
/*     */     }
/* 117 */     String sub3Path = "/inbox/sub3";
/* 118 */     ZFolder sub3 = ownerMbx.getFolderByPath(sub3Path);
/* 119 */     if (sub3 == null) {
/* 120 */       sub3 = TestUtil.createFolder(ownerMbx, sub3Path);
/* 121 */       ownerMbx.modifyFolderGrant(sub3.getId(), ZGrant.GranteeType.pub, null, "r", null);
/*     */     }
/*     */     
/* 124 */     this.OWNER_ACCT_ID = ownerAcct.getId();
/* 125 */     this.USER1 = prov.get(Key.AccountBy.name, "user1");
/* 126 */     this.USER2 = prov.get(Key.AccountBy.name, "user2");
/* 127 */     this.USER3 = prov.get(Key.AccountBy.name, "user3");
/*     */     
/* 129 */     this.INBOX_FID = Integer.valueOf(inbox.getId()).intValue();
/* 130 */     this.SUB1_FID = Integer.valueOf(sub1.getId()).intValue();
/* 131 */     this.SUB2_FID = Integer.valueOf(sub2.getId()).intValue();
/* 132 */     this.SUB3_FID = Integer.valueOf(sub3.getId()).intValue();
/*     */   }
/*     */   
/*     */   private String formatRights(short rights) {
/* 136 */     return ACL.rightsToString(rights) + "(" + rights + ")";
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void doTest(Account authedAcct, String ownerAcctId, int targetFolderId, short expectedEffectivePermissions, short needRightsForCanAccessTest, boolean expectedCanAccess)
/*     */     throws ServiceException
/*     */   {
/*     */     OperationContext octxt;
/*     */     
/*     */ 
/*     */     OperationContext octxt;
/*     */     
/* 149 */     if (authedAcct == null) {
/* 150 */       octxt = null;
/*     */     } else {
/* 152 */       octxt = new OperationContext(authedAcct);
/*     */     }
/* 154 */     FolderACL folderAcl = new FolderACL(octxt, ownerAcctId, targetFolderId);
/*     */     
/* 156 */     short effectivePermissions = folderAcl.getEffectivePermissions();
/* 157 */     boolean canAccess = folderAcl.canAccess(needRightsForCanAccessTest);
/*     */     
/* 159 */     boolean good = false;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 166 */     short actual = effectivePermissions;
/* 167 */     short expected = expectedEffectivePermissions;
/* 168 */     if (actual == -1)
/* 169 */       actual = ACL.stringToRights(ACL.rightsToString(actual));
/* 170 */     if (expected == -1) {
/* 171 */       expected = ACL.stringToRights(ACL.rightsToString(expected));
/*     */     }
/* 173 */     assertEquals(expected, actual);
/* 174 */     assertEquals(expectedCanAccess, canAccess);
/*     */     
/* 176 */     good = true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testPublicUser()
/*     */     throws Exception
/*     */   {
/* 186 */     doTest(GuestAccount.ANONYMOUS_ACCT, this.OWNER_ACCT_ID, this.INBOX_FID, (short)0, (short)1, false);
/* 187 */     doTest(GuestAccount.ANONYMOUS_ACCT, this.OWNER_ACCT_ID, this.SUB1_FID, (short)0, (short)1, false);
/* 188 */     doTest(GuestAccount.ANONYMOUS_ACCT, this.OWNER_ACCT_ID, this.SUB2_FID, (short)0, (short)1, false);
/*     */     
/* 190 */     doTest(GuestAccount.ANONYMOUS_ACCT, this.OWNER_ACCT_ID, this.SUB3_FID, (short)1, (short)1, true);
/* 191 */     doTest(GuestAccount.ANONYMOUS_ACCT, this.OWNER_ACCT_ID, this.SUB3_FID, (short)1, (short)2, false);
/*     */   }
/*     */   
/*     */   public void testOwner()
/*     */     throws Exception
/*     */   {
/* 197 */     doTest(null, this.OWNER_ACCT_ID, this.INBOX_FID, (short)-1, (short)-1, true);
/* 198 */     doTest(null, this.OWNER_ACCT_ID, this.SUB1_FID, (short)-1, (short)-1, true);
/* 199 */     doTest(null, this.OWNER_ACCT_ID, this.SUB2_FID, (short)-1, (short)-1, true);
/* 200 */     doTest(null, this.OWNER_ACCT_ID, this.SUB3_FID, (short)-1, (short)-1, true);
/*     */   }
/*     */   
/*     */   public void testUser1() throws Exception
/*     */   {
/* 205 */     doTest(this.USER1, this.OWNER_ACCT_ID, this.INBOX_FID, (short)-1, (short)-1, true);
/* 206 */     doTest(this.USER1, this.OWNER_ACCT_ID, this.SUB1_FID, (short)-1, (short)-1, true);
/* 207 */     doTest(this.USER1, this.OWNER_ACCT_ID, this.SUB2_FID, (short)-1, (short)-1, true);
/* 208 */     doTest(this.USER1, this.OWNER_ACCT_ID, this.SUB3_FID, (short)-1, (short)-1, true);
/*     */   }
/*     */   
/*     */   public void testUser2() throws Exception {
/* 212 */     doTest(this.USER2, this.OWNER_ACCT_ID, this.INBOX_FID, (short)0, (short)2, false);
/* 213 */     doTest(this.USER2, this.OWNER_ACCT_ID, this.SUB1_FID, (short)2, (short)2, true);
/* 214 */     doTest(this.USER2, this.OWNER_ACCT_ID, this.SUB2_FID, (short)2, (short)2, true);
/*     */     
/* 216 */     doTest(this.USER2, this.OWNER_ACCT_ID, this.SUB3_FID, (short)1, (short)1, true);
/* 217 */     doTest(this.USER2, this.OWNER_ACCT_ID, this.SUB3_FID, (short)1, (short)2, false);
/*     */   }
/*     */   
/*     */   public void testUser3() throws Exception {
/* 221 */     doTest(this.USER3, this.OWNER_ACCT_ID, this.INBOX_FID, (short)0, (short)2, false);
/* 222 */     doTest(this.USER3, this.OWNER_ACCT_ID, this.SUB1_FID, (short)3, (short)2, true);
/* 223 */     doTest(this.USER3, this.OWNER_ACCT_ID, this.SUB2_FID, (short)3, (short)2, true);
/*     */     
/* 225 */     doTest(this.USER3, this.OWNER_ACCT_ID, this.SUB3_FID, (short)1, (short)1, true);
/* 226 */     doTest(this.USER3, this.OWNER_ACCT_ID, this.SUB3_FID, (short)1, (short)2, false);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws ServiceException
/*     */   {
/* 231 */     DbPool.startup();
/* 232 */     MemcachedConnector.startup();
/*     */     
/* 234 */     CliUtil.toolSetup();
/* 235 */     TestUtil.runTest(TestFolderACLCache.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestFolderACLCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */